package com.cndym.dao.impl;


import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.cndym.bean.Cooperation;
import com.cndym.dao.ICooperationDao;
import com.cndym.utils.Utils;

@Repository
public class CooperationDaoImpl implements ICooperationDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;


    public Cooperation getCooperationBySid(String sid) {
    	
    	String sql = "select * from user_cooperation where sid=?";
    	List list = jdbcTemplate.query(sql, new BeanPropertyRowMapper  (Cooperation.class), new Object[]{sid});

        if (list.isEmpty()) {
            return null;
        }
        return (Cooperation)list.get(0);
    }

    public Cooperation getCooperationById(Long id) {
    	
    	String sql = "select * from user_cooperation where id=?";
    	List list = jdbcTemplate.query(sql, new BeanPropertyRowMapper  (Cooperation.class), new Object[]{id});
        if (list.isEmpty()) {
            return null;
        }
        return (Cooperation)list.get(0);
    }

    

    public boolean doUpdateStauts(String sid, Integer status) {
        String sql = "update user_cooperation set status = ? where sid = ?";
        try {
            jdbcTemplate.update(sql, new Object[]{status, sid});
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


    public List<Cooperation> getCooperation(Cooperation cooperation, String mask) {
        StringBuffer sql = new StringBuffer("From Cooperation where 1 = 1 ");
        
        return null;
    }

    


    public Map getLastRegisterCooperation() {
        String sql = "select sid,company_name as companyName,company_website as companyWebsite from user_cooperation where id in (select max(id) from user_cooperation)";
        List dataList = jdbcTemplate.queryForList(sql);
        if (dataList != null && dataList.size() > 0) {
            return (Map) dataList.get(0);
        }
        return null;
    }

    @Override
    public String getMaxCooperationSidOfMask(String mask) {
        String sql = "select max(sid) as sid from user_cooperation where 1=1 ";
        if (Utils.isNotEmpty(mask)) {
            sql += "and sid LIKE '" + mask + "'";
        }
        Map<String, Object> map = jdbcTemplate.queryForMap(sql);
        try {
            return String.valueOf(map.get("sid"));
        } catch (Exception e) {
            return null;
        }
    }

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
}